﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TpBaseObjects.Properties;
using Microsoft.Office.Interop.Excel;
using TpExtensions;
using TpLogger;
using System.IO;

namespace TpBaseObjects.Base {

    public class ProjectRow {
        
        #region Members

        private Range myRow;
        private int startRow = 0;

        #endregion

        #region Contructor

        public ProjectRow(Range row) {
            myRow = row;
        }

        #endregion

        #region Properties

        public bool BildeSumme {
            get {

                if(RowType == "S4") {
                    return true;
                }

                if(RowTerm == null | RowTerm == string.Empty) {
                    return false;
                }

                if(RowTerm.Contains("S")) {
                    return true;
                }

                Range r1 = myRow.Cells[1, 10]; //Wegen Kompbtibilität mit vorigen Versionen
                string s1 = Convert.ToString(r1.Value);
                if(Extensions.Nz(s1, "").Contains("S")) {
                    return true;
                }

                return false;
            }
        }

        public Nullable<DateTime> ChangeDate {
            get {
                Range r1 = myRow.Cells[1, "AZ"];
                DateTime ret = new DateTime(1900, 1, 1);
                if (r1.Value != null) {
                    try {
                        ret = Convert.ToDateTime(r1.Value);
                    } catch {
                        r1.Interior.Color = Settings.Default.Gelb;
                        return null;
                    }
                }
                return ret;
            }
        }

        public Double ChangeBeginTime {
            get {
                Range r1 = myRow.Cells[1, "BA"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public Double ChangeEndTime {
            get {
                Range r1 = myRow.Cells[1, "BB"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public Double ChangeFlatTime {
            get {
                Range r1 = myRow.Cells[1, "BD"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public Double ChangeQuantity {
            get {
                Range r1 = myRow.Cells[1, "BE"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public Double ChangePerformance {
            get {
                Range r1 = myRow.Cells[1, "BC"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public string ChangeType {
            get {
                Range r1 = myRow.Cells[1, "AY"];
                string t1 =  Convert.ToString(r1.Value);
                return Extensions.Nz(t1.ToUpper(), "");
            }
            set {
                Range r1 = myRow.Cells[1, "AY"];
                r1.Value = value;
            }
        }

        public int DeleteRowNumber {
            get {
                return startRow;
            }
        }

        public string Durchführender1 {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender1];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value),"");
                if(!Extensions.TpUser.Contains(t1)) {
                    t1 = "";
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender1];
                r1.Value = value;
            }
        }

        public string Durchführender2 {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender2];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                if(!Extensions.TpUser.Contains(t1)) {
                    t1 = "";
                }
                return t1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteDurchführender2];
                r1.Value = value;
            }
        }

        public string Einheit {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteEinheit];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
            
        }

        public string Fahrzeug {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteFahrzeug];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteFahrzeug];
                r1.Value = value;
            }
        }

        public string Hyperlink {
            get {
                string ret = "";
                Range r1 = myRow.Cells["J"];
                string t1 = Convert.ToString(r1.Value);
                if (t1 != null) {
                    ret = t1;
                }
                return ret;
            }
            set {
                string address;
                Range r3 = myRow.Cells[1, "J"];
                if (value != null) {
                    address = value;
                    r3.Value = "";
                    if (!String.IsNullOrEmpty(address)) {
                        string display = new FileInfo(address).Name;
                        r3.Hyperlinks.Add(r3, Address: address, TextToDisplay: display);
                    }
                } else {
                    r3.Value = "";
                }

            }
        }

        public bool IsEpRow {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeEp];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0) != 0;
            }
        }

        public bool IsLeOrLa {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteZeilenArt];
                string t1 = Convert.ToString(r1.Value);
                return (Extensions.Nz(t1, "").ToUpper() == "LE" | Extensions.Nz(t1, "").ToUpper() == "LA" | Extensions.Nz(t1, "").ToUpper() == "AU");
            }
        }

        public bool IsProjectJob {
            get {
                bool isProJob;
                string cellValue = Extensions.Nz(Convert.ToString(Spalte("G")),"");
                string arValue = Extensions.Nz(Convert.ToString(Spalte("AR")), "");
                isProJob = (cellValue == "Kund" | cellValue == "Verk" | cellValue == "Verl");
                if (!isProJob) {
                    isProJob = arValue != "";
                }
                return isProJob;
            }
        }

        public bool IsRabattZeile {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteZeilenArt];
                string t1 = Convert.ToString(r1.Value);
                if(Extensions.Nz(t1, "").ToUpper() == "RA") {
                    return true;
                }
                return false;
            }
        }

        public bool IsReadyForExport {
            get {
                bool r1 = ((JobDatum <= DateTime.Today & !LeistungExported) & !JobExported);
                return r1;
            }
        }

        public bool IsReadyForJobevidenz {
            get {
                bool r1 = JobDatum > DateTime.Today & !JobExported;
                return r1;
            }
        }

        public bool IsReadyForInternalJobevidenz {
            get {
                bool r1 = JobDatum > DateTime.Today.AddDays(-1) & !JobExported;
                return r1;
            }
        }

        public bool IsReadyToStorno {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                return t1.ToUpper() == "S";
            }
        }

        public bool IsPauschalTime {
            get {
                Range r1 = myRow.Cells[1, "AL"];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                return t1.ToUpper() == "PA";
            }
        }

        public bool IsSummenZeile {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeVp];
                string t1 = Convert.ToString(r1.FormulaR1C1);
                if(Extensions.Nz(t1, "").Contains("SUM")) {
                    return true;
                }
                return false;
            }
        }

        public bool IsVpRow {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeVp];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0) != 0;
            }
        }

        public DateTime JobDatum {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteJobDatum];
                DateTime ret = new DateTime(1900, 1, 1);
                if(r1.Value != null) {
                    ret = Convert.ToDateTime(r1.Value);
                }
                return ret;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteJobDatum];
                if (value != new DateTime(1900, 1, 1)) {
                    r1.Value = value.ToString("yyyy/MM/dd");
                } else {
                    r1.Value = "";
                }
            }
        }

        public bool JobExported {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value),"");
                return t1.ToUpper() == "JE";
            }
        }

        public string JobTime {
            get {
                string s1 = "0";
                Range r1 = myRow.Cells[1, "P"];
                if(r1.Value != null) {
                    s1 = Extensions.Nz(Convert.ToString(r1.Value), "0");
                }
                return s1;
            }
            set {
                string s1 = value;
                Range r1 = myRow.Cells[1, "P"];
                r1.Value = s1;
            }
        }

        public string JobEndTime {
            get {
                string s1 = "0";
                Range r1 = myRow.Cells[1, "Q"];
                if (r1.Value != null) {
                    s1 = Extensions.Nz(Convert.ToString(r1.Value), "0");
                }
                return s1;
            }
            set {
                string s1 = value;
                Range r1 = myRow.Cells[1, "Q"];
                r1.Value = s1;
            }
        }

        public string KundenName {
            get {
                Range r1 = myRow.Cells[1, 3];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public string Leistung {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteText];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteText];
                r1.Value = value;
            }
        }

        public string JobOrt {
            get {
                Range r1 = myRow.Cells[1, "M"];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
            set {
                Range r1 = myRow.Cells[1, "M"];
                r1.Value = value;
            }
        }

        public bool LeistungExported {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
                string t1 = Extensions.Nz(Convert.ToString(r1.Value),"");
                return t1.ToUpper() == "LE";
            }
        }

        public double LeistungProzent {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungProzent];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungProzent];
                r1.Value = value;
            }
        }

        public double LeistungZeit {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungZeit];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistungZeit];
                r1.Value = value;
            }
        }

        public double PauschalZeit {
            get {
                Range r1 = myRow.Cells[1, "U"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, "U"];
                r1.Value = value;
            }
        }

        public double LeistungZeitSumme {
            get {
                Range r1 = myRow.Cells[1, "AV"];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, "AV"];
                r1.Value = value;
            }
        }

        public double Menge {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteMenge];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteMenge];
                r1.Value = value;
            }
        }

        public Range MyRow {
            get { return myRow; }
        }

        public string ProjectName {
            get {
                Range r1 = myRow.Cells[1, 4];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public double RabattProzent {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteRabatt];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public bool RechnungExported {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "").ToUpper() == "AR";
            }
        }

        public int RechnungsNummer {
            get {
                Range r1 = myRow.Cells[1, "S"];
                string t1 = Convert.ToString(r1.Value);
                int d1 = 0;
                if (t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToInt32 (r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
            set {
                Range r1 = myRow.Cells[1, "S"];
                r1.Value = value;
            }
        }

        public string RowLeistung {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistung];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteLeistung];
                r1.Value = value;
            }
        }

        public int RowNumber {
            get;
            set;
        }

        public string RowTerm {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteZwischensummen];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public string RowText {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteText];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public string RowType {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteZeilenArt];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public string S4Type {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummenText];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public string Stellvertreter {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteStellvertreter];
                string s1 = Extensions.Nz(Convert.ToString(r1.Value), "");
                if(!Extensions.TpUser.Contains(s1)) {
                    s1 = "";
                }
                return s1;
            }
            set {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteStellvertreter];
                string t1 = Extensions.Nz(Convert.ToString(value), "");
                if(Extensions.TpUser.Contains(t1) | value == "") {
                    r1.Value = value;
                }
            }
        }

        public double SummeEp {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeEp];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public int SummenHierachie {
            get {
                if(RowType == "S4") {
                    return 4;
                }
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeVp];
                if(Extensions.Nz(r1.FormulaR1C1, "") != "") {
                    if(Extensions.Right(r1.FormulaR1C1, 1) == ")") {
                        return 1;
                    }
                    if(Extensions.Right(r1.FormulaR1C1, 2) == "/2") {
                        return 2;
                    }
                    if(Extensions.Right(r1.FormulaR1C1, 2) == "/3") {
                        return 3;
                    }
                    if(Extensions.Right(r1.FormulaR1C1, 2) == "/4") {
                        return 4;
                    }
                }
                return 0;
            }
        }

        public string SummenText {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummenText];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "");
            }
        }

        public double SummeVp {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummeVp];
                string t1 = Convert.ToString(r1.Value);
                double d1 = 0;
                if(t1.IsNumeric(System.Globalization.NumberStyles.Any)) {
                    d1 = Convert.ToDouble(r1.Value);
                }
                return Extensions.Nz(d1, 0);
            }
        }

        public bool Ust {
            get {
                Range r1 = myRow.Cells[1, Settings.Default.SpalteSummenText];
                string t1 = Convert.ToString(r1.Value);
                return Extensions.Nz(t1, "").ToUpper() == "UMSATZSTEUER";
            }
        }

        #endregion

        #region Methods

        public void ClearContent() {
            MyRow.ClearContents();
            MyRow.ClearComments();
            MyRow.Font.Bold = false;
        }

        public void MarkAsFailed() {
            myRow.Interior.Color = Settings.Default.Gelb;
        }

        public void ResetJobDatum() {
            Range r1 = myRow.Cells[1, Settings.Default.SpalteJobDatum];
            r1.Value = "";
        }

        public void SetExported(string export) {
            Range r1 = myRow.Cells[1, Settings.Default.SpalteExported];
            r1.Value = export;
        }

        public void SetFontBold(bool bold) {
            Range r1 = myRow.Rows[1];
            //r1.Select();
            r1.Font.Bold = bold;
        }

        public void ClearTpCells(Logger log) {
            try {
                Range r1 = myRow.Cells[1, "E"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "F"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "G"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "H"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "AW"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "AX"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "AY"];
                r1.ClearContents();
                r1 = myRow.Cells[1, "AZ"];
                r1.ClearContents();

            } catch(Exception ex) {
                log.Add("ClearTpCells", ex);
            }
        }

        #endregion

        #region Functions

        public object Spalte(string spaltenName) {
            Range r1 = myRow.Cells[1, spaltenName];
            object retObj = "";
            object rowObj = r1.Value;
            if(r1.Value != null) {
                if(Extensions.isNumeric(rowObj.ToString(), System.Globalization.NumberStyles.Any)) {
                    retObj = r1.Value;
                    return retObj;
                } else {
                    return Extensions.Nz(r1.Value, "");
                }
            }
            return retObj;
        }

        #endregion

        #region Overrides

        public override bool Equals(object obj) {
            bool equals = false;
            ProjectRow objRow = obj as ProjectRow;
            if(objRow.Durchführender1 == this.Durchführender1 && objRow.Durchführender2 == this.Durchführender2
                && objRow.Einheit == this.Einheit && objRow.Fahrzeug == this.Fahrzeug
                && objRow.IsEpRow == this.IsEpRow && objRow.IsLeOrLa == this.IsLeOrLa
                && objRow.IsRabattZeile == this.IsRabattZeile && objRow.IsSummenZeile == this.IsSummenZeile
                && objRow.IsVpRow == this.IsVpRow && objRow.JobDatum == this.JobDatum
                && objRow.KundenName == this.KundenName && objRow.Leistung == this.Leistung
                && objRow.LeistungProzent == this.LeistungProzent && objRow.LeistungZeit == this.LeistungZeit
                && objRow.Menge == this.Menge && objRow.RabattProzent == this.RabattProzent
                && objRow.RowLeistung == this.RowLeistung && objRow.RowTerm == this.RowTerm
                && objRow.RowText == this.RowText && objRow.RowType == this.RowType
                && objRow.SummenText == this.SummenText) {
                    equals = true;
                
            }
            return equals;
        }

	  public override int GetHashCode()
	  {
		  return base.GetHashCode();
	  }

        #endregion

    }
}
